/*
 * Copyright (C) 2010-2011 Graz University of Technology, Austria. All Rights reserved. 
 *
 * Contact: Simone Kopeinik <simone.kopeinik@tugraz.at>
 * 	   Graz University of Technology, Knowledge Management Institute, Brückenkopfgasse 1/6, 8020 Graz 	
 * 	   <http://www.kmi.tugraz.at/>
 * 
 * This software is part of the TARGET platform developed by the
 * TARGET EC-funded project www.reachyourtarget.org
 *
 * This software is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, version 3.
 *
 * This software is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * The GNU General Public License is available at <http://www.gnu.org/licenses/>.
 *
 */
package at.tugraz.kmi.css.analyzer.gapvis.koichi;

import java.awt.Component;
import java.awt.Shape;
import java.awt.event.InputEvent;
import java.awt.event.MouseEvent;
import java.awt.geom.CubicCurve2D;
import java.awt.geom.Point2D;

import javax.swing.JPopupMenu;

import org.apache.commons.collections15.Factory;

import at.tugraz.kmi.css.analyzer.gapvis.koichi.events.MenuPointListener;
import at.tugraz.kmi.css.analyzer.gapvis.koichi.events.VertexMenuListener;
import edu.uci.ics.jung.algorithms.layout.GraphElementAccessor;
import edu.uci.ics.jung.graph.util.EdgeType;
import edu.uci.ics.jung.visualization.VisualizationServer;
import edu.uci.ics.jung.visualization.VisualizationViewer;
import edu.uci.ics.jung.visualization.control.AbstractPopupGraphMousePlugin;

public class PopupMenuMousePlugin<UserCompetence, GraphEdge> extends
		AbstractPopupGraphMousePlugin {
	private JPopupMenu						vertexPopup;
	protected UserCompetence				startVertex;
	protected CubicCurve2D					rawEdge	= new CubicCurve2D.Float();
	protected Shape							edgeShape;
	protected Shape							rawArrowShape;
	protected Shape							arrowShape;
	protected VisualizationServer.Paintable	edgePaintable;
	protected VisualizationServer.Paintable	arrowPaintable;
	protected EdgeType						edgeIsDirected;
	protected Factory<UserCompetence>		vertexFactory;
	protected Factory<GraphEdge>			edgeFactory;

	public PopupMenuMousePlugin() {
		super(InputEvent.BUTTON3_MASK);
	}

	@Override
	protected void handlePopup(MouseEvent e) {
		Object source = e.getSource();
		Point2D p = e.getPoint();

		VisualizationViewer<UserCompetence, GraphEdge> viewer = (VisualizationViewer<UserCompetence, GraphEdge>) source;
		GraphElementAccessor<UserCompetence, GraphEdge> pickSupport = viewer
				.getPickSupport();
		if (pickSupport != null) {
			UserCompetence v = pickSupport.getVertex(viewer.getGraphLayout(),
					p.getX(), p.getY());
			if (v != null) {
				updateVertexMenu(v, viewer, p);
				vertexPopup.show(viewer, e.getX(), e.getY());
			}
		}
	}

	private void updateVertexMenu(UserCompetence v, VisualizationViewer vv,
			Point2D point) {
		if (vertexPopup == null) {
			return;
		}
		Component[] components = vertexPopup.getComponents();
		for (Component component : components) {
			if (component instanceof VertexMenuListener) {
				((VertexMenuListener) component).setVertexAndView(v, vv);
			}
			if (component instanceof MenuPointListener) {
				((MenuPointListener) component).setPoint(point);
			}
		}

	}

	public JPopupMenu getVertexPopup() {
		return vertexPopup;
	}

	public void setVertexPopup(JPopupMenu vertexPopup) {
		this.vertexPopup = vertexPopup;
	}
}
